package com.tongji.housediy.view
{
	import com.tongji.housediy.events.TypeXMLAdminEvent;
	import com.tongji.housediy.events.goodsEvent.FilterGoodsEvent;
	import com.tongji.housediy.events.goodsEvent.GetTypeEvent;
	import com.tongji.housediy.model.ListTypeModel;
	import com.tongji.housediy.model.RightListStateModel;
	
	import mx.binding.utils.BindingUtils;
	import mx.events.FlexEvent;
	import mx.events.ListEvent;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class CustomDropDownListMediator extends Mediator
	{
		[Inject]
		public var view:CustomDropDownList;
		
		[Inject]
		public var model:ListTypeModel;
		
		override public function onRegister():void
		{
			addContextListener(GetTypeEvent.UPDATE_TYPE, updateXML, GetTypeEvent);
			/*switch (view.id)
			{
				case "doorList":
					BindingUtils.bindProperty(view, "dataProvider", model, "doorXMLList");
					break;
				case "windowList":
					BindingUtils.bindProperty(view, "dataProvider", model, "windowXMLList");
					break;
				case "floorList1":
					BindingUtils.bindProperty(view, "dataProvider", model, "floorLevel1XMLList");
					break;
				case "furnitureList1":
					BindingUtils.bindProperty(view, "dataProvider", model, "furnitureLevel1XMLList");
					break;
				case "wallTypeList1":
					BindingUtils.bindProperty(view, "dataProvider", model, "wallLevel1XMLList");
					break;
			}
			view.selectedIndex = 0;
			
			view.addEventListener(IndexChangeEvent.CHANGE, viewChangedHandler);
			view.addEventListener(FlexEvent.VALUE_COMMIT, valueCommitHandler);*/
		}
		
		private function updateXML(event:GetTypeEvent):void
		{
			switch (view.id)
			{
				case "doorList":
					BindingUtils.bindProperty(view, "dataProvider", model, "doorXMLList");
					break;
				case "windowList":
					BindingUtils.bindProperty(view, "dataProvider", model, "windowXMLList");
					break;
				case "floorList1":
					BindingUtils.bindProperty(view, "dataProvider", model, "floorLevel1XMLList");
					break;
				case "furnitureList1":
					BindingUtils.bindProperty(view, "dataProvider", model, "furnitureLevel1XMLList");
					break;
				case "wallTypeList1":
					BindingUtils.bindProperty(view, "dataProvider", model, "wallLevel1XMLList");
					break;
			}
			view.selectedIndex = 0;
			view.addEventListener(IndexChangeEvent.CHANGE, viewChangedHandler);
			view.addEventListener(FlexEvent.VALUE_COMMIT, valueCommitHandler);
		}
		private function viewChangedHandler(event:IndexChangeEvent):void
		{
			switch (view.id)
			{
				case "doorList":
				case "windowList":
					dispatch(new FilterGoodsEvent(FilterGoodsEvent.FILTER_GOODS, view.id, [view.selectedItem.@typeID.toString()]));
					break;
				case "floorList1":
					dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.UPDATE_FLOOR_LEVEL2_XML, view.selectedItem.toString()));
					break;
				case "furnitureList1":
					dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.UPDATE_FURNITURE_LEVEL2_XML, view.selectedItem.toString()));
					break;
				case "wallTypeList1":
					dispatch(new TypeXMLAdminEvent(TypeXMLAdminEvent.UPDATE_WALL_LEVEL2_XML, view.selectedItem.toString()));
					break;
			}
			
			event.stopImmediatePropagation();
		}
		
		private function valueCommitHandler(event:FlexEvent):void
		{
			view.selectedIndex = 0;
		}
	}
}